/**
 * 作者: wgx
 * 日期: 2024-10-30 11:38:46
 * 备注: 用户城镇信息表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserCityInfo;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserCityInfoModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 用户ID
    protected int peasonNum; // 人口(居民+无居者), (获得人口时增加这个值，不能超过user_exp配置的上限)
    protected int peasonMax; // 居民楼的数量 (摆放修复房子时会增加，拆除时会减少)
    protected String cityName = ""; // 城市名
    protected long dailyDiamondTime; // 领取每日钻石奖励的时间
    protected int isInitCoord; // 是否已加载初始地面坐标 (1-已加载)
    protected long bankRewardTime; // 金库自动点击奖励的时间
    protected int soldierWound; // 未进行恢复的伤兵 (当前兵力 = 有居所的居民 - 伤员 - 战斗兵力)
    protected int soldierBattle; // 派出去战斗的兵力
    protected int radarLevelReward; // 雷达升级奖励领取到哪个等级

    public void reset() {
    }

    protected String getKey() {
        return "UserCityInfo." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_city_info"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 用户ID
        obj.put("peasonNum", peasonNum); // 人口(居民+无居者), (获得人口时增加这个值，不能超过user_exp配置的上限)
        obj.put("peasonMax", peasonMax); // 居民楼的数量 (摆放修复房子时会增加，拆除时会减少)
        obj.put("cityName", cityName); // 城市名
        obj.put("dailyDiamondTime", dailyDiamondTime); // 领取每日钻石奖励的时间
        obj.put("isInitCoord", isInitCoord); // 是否已加载初始地面坐标 (1-已加载)
        obj.put("bankRewardTime", bankRewardTime); // 金库自动点击奖励的时间
        obj.put("soldierWound", soldierWound); // 未进行恢复的伤兵 (当前兵力 = 有居所的居民 - 伤员 - 战斗兵力)
        obj.put("soldierBattle", soldierBattle); // 派出去战斗的兵力
        obj.put("radarLevelReward", radarLevelReward); // 雷达升级奖励领取到哪个等级
        return obj;
    }

    public PBUserCityInfo.Builder takeInitBuilder() {
        PBUserCityInfo.Builder sb = PBUserCityInfo.newBuilder();
        if (peasonNum != 0) {
            sb.setPeasonNum(peasonNum);
        }
        if (peasonMax != 0) {
            sb.setPeasonMax(peasonMax);
        }
        if (!cityName.isEmpty()) {
            sb.setCityName(cityName);
        }
        if (dailyDiamondTime != 0) {
            sb.setDailyDiamondTime(dailyDiamondTime);
        }
        if (isInitCoord != 0) {
            sb.setIsInitCoord(isInitCoord);
        }
        if (soldierWound != 0) {
            sb.setSoldierWound(soldierWound);
        }
        if (soldierBattle != 0) {
            sb.setSoldierBattle(soldierBattle);
        }
        if (radarLevelReward != 0) {
            sb.setRadarLevelReward(radarLevelReward);
        }
        return sb;
    }

    public PBUserCityInfo.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserCityInfo.Builder sb = PBUserCityInfo.newBuilder();
        if (isFieldUpdate("peasonNum")) {
            sb.setPeasonNum(peasonNum);
        }
        if (isFieldUpdate("peasonMax")) {
            sb.setPeasonMax(peasonMax);
        }
        if (isFieldUpdate("cityName")) {
            sb.setCityName(cityName);
        }
        if (isFieldUpdate("dailyDiamondTime")) {
            sb.setDailyDiamondTime(dailyDiamondTime);
        }
        if (isFieldUpdate("isInitCoord")) {
            sb.setIsInitCoord(isInitCoord);
        }
        if (isFieldUpdate("soldierWound")) {
            sb.setSoldierWound(soldierWound);
        }
        if (isFieldUpdate("soldierBattle")) {
            sb.setSoldierBattle(soldierBattle);
        }
        if (isFieldUpdate("radarLevelReward")) {
            sb.setRadarLevelReward(radarLevelReward);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getPeasonNum() {
        return peasonNum;
    }

    public void putPeasonNum(int peasonNum) {
        if (this.peasonNum != peasonNum) {
            this.peasonNum = peasonNum;
            updateRedisHashField(getKey(), "peasonNum", this.peasonNum);
        }
    }

    public void addPeasonNum(int value) {
        this.peasonNum += value;
        updateRedisHashField(getKey(), "peasonNum", this.peasonNum);
    }

    public int getPeasonMax() {
        return peasonMax;
    }

    public void putPeasonMax(int peasonMax) {
        if (this.peasonMax != peasonMax) {
            this.peasonMax = peasonMax;
            updateRedisHashField(getKey(), "peasonMax", this.peasonMax);
        }
    }

    public void addPeasonMax(int value) {
        this.peasonMax += value;
        updateRedisHashField(getKey(), "peasonMax", this.peasonMax);
    }

    public String getCityName() {
        return cityName;
    }

    public void putCityName(String cityName) {
        if (!this.cityName.equals(cityName)) {
            this.cityName = cityName;
            updateRedisHashField(getKey(), "cityName", this.cityName);
        }
    }

    public long getDailyDiamondTime() {
        return dailyDiamondTime;
    }

    public void putDailyDiamondTime(long dailyDiamondTime) {
        if (this.dailyDiamondTime != dailyDiamondTime) {
            this.dailyDiamondTime = dailyDiamondTime;
            updateRedisHashField(getKey(), "dailyDiamondTime", this.dailyDiamondTime);
        }
    }

    public void addDailyDiamondTime(long value) {
        this.dailyDiamondTime += value;
        updateRedisHashField(getKey(), "dailyDiamondTime", this.dailyDiamondTime);
    }

    public int getIsInitCoord() {
        return isInitCoord;
    }

    public void putIsInitCoord(int isInitCoord) {
        if (this.isInitCoord != isInitCoord) {
            this.isInitCoord = isInitCoord;
            updateRedisHashField(getKey(), "isInitCoord", this.isInitCoord);
        }
    }

    public void addIsInitCoord(int value) {
        this.isInitCoord += value;
        updateRedisHashField(getKey(), "isInitCoord", this.isInitCoord);
    }

    public long getBankRewardTime() {
        return bankRewardTime;
    }

    public void putBankRewardTime(long bankRewardTime) {
        if (this.bankRewardTime != bankRewardTime) {
            this.bankRewardTime = bankRewardTime;
            updateRedisHashField(getKey(), "bankRewardTime", this.bankRewardTime);
        }
    }

    public void addBankRewardTime(long value) {
        this.bankRewardTime += value;
        updateRedisHashField(getKey(), "bankRewardTime", this.bankRewardTime);
    }

    public int getSoldierWound() {
        return soldierWound;
    }

    public void putSoldierWound(int soldierWound) {
        if (this.soldierWound != soldierWound) {
            this.soldierWound = soldierWound;
            updateRedisHashField(getKey(), "soldierWound", this.soldierWound);
        }
    }

    public void addSoldierWound(int value) {
        this.soldierWound += value;
        updateRedisHashField(getKey(), "soldierWound", this.soldierWound);
    }

    public int getSoldierBattle() {
        return soldierBattle;
    }

    public void putSoldierBattle(int soldierBattle) {
        if (this.soldierBattle != soldierBattle) {
            this.soldierBattle = soldierBattle;
            updateRedisHashField(getKey(), "soldierBattle", this.soldierBattle);
        }
    }

    public void addSoldierBattle(int value) {
        this.soldierBattle += value;
        updateRedisHashField(getKey(), "soldierBattle", this.soldierBattle);
    }

    public int getRadarLevelReward() {
        return radarLevelReward;
    }

    public void putRadarLevelReward(int radarLevelReward) {
        if (this.radarLevelReward != radarLevelReward) {
            this.radarLevelReward = radarLevelReward;
            updateRedisHashField(getKey(), "radarLevelReward", this.radarLevelReward);
        }
    }

    public void addRadarLevelReward(int value) {
        this.radarLevelReward += value;
        updateRedisHashField(getKey(), "radarLevelReward", this.radarLevelReward);
    }

}